package com.lumeijin.controller;

import com.lumeijin.entity.User;
import com.lumeijin.service.SmsService;
import com.lumeijin.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.Optional;

@Controller
@RequiredArgsConstructor
public class UserController {
    private final UserService userService;
    private final SmsService smsService;
    @GetMapping("/")
    public String index() {
        return "redirect:/register";
    }
    
    @GetMapping("/register")
    public String registerPage() {
        return "register";
    }
    
    @PostMapping("/sendSmsCode")
    @ResponseBody
    public String sendSmsCode(@RequestParam String phone) {
        if (!phone.matches("^1[3-9]\\d{9}$")) {
            return "手机号格式不正确";
        }
        if (!smsService.canSendCode(phone)) {
            return "请1分钟后再试";
        }
        String code = smsService.sendSmsCode(phone);
        return "验证码已发送，验证码为: " + code + "（测试用）";
    }
    
    @PostMapping("/register")
    @ResponseBody
    public String register(@RequestParam String username,
                          @RequestParam String password,
                          @RequestParam String email,
                          @RequestParam String phone,
                          @RequestParam String smsCode) {
        String result = userService.registerUser(username, password, email, phone, smsCode);
        return result;
    }
    
    @GetMapping("/login")
    public String loginPage() {
        return "login";
    }
    
    @PostMapping("/login")
    @ResponseBody
    public String login(@RequestParam String username,
                       @RequestParam String password,
                       HttpSession session) {
        Optional<User> userOpt = userService.findByUsername(username);
        if (userOpt.isPresent()) {
            User user = userOpt.get();
            if (userService.verifyPassword(password, user.getPassword())) {
                session.setAttribute("user", user);
                return "登录成功";
            } else {
                return "密码错误";
            }
        } else {
            return "用户不存在";
        }
    }
    
    @GetMapping("/profile")
    public String profile(HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/login";
        }
        Optional<User> userOpt = userService.findById(user.getId());
        if (userOpt.isPresent()) {
            model.addAttribute("user", userOpt.get());
        }
        return "profile";
    }
    
    @PostMapping("/uploadAvatar")
    @ResponseBody
    public String uploadAvatar(@RequestParam("file") MultipartFile file,
                              HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "请先登录";
        }
        return userService.uploadAvatar(user.getId(), file);
    }
    
    @GetMapping("/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("user");
        return "redirect:/register";
    }
    
}
